package org.yx.service.sbas.save_client_roster;

import org.yx.exception.BizException;
import org.yx.models.SbasCusDef;
import org.yx.models.SbasSupDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

/**
 * 保存客户信息
 * @author zxw
 * @since 2018/03/27
**/
public class SbasSaveClientRosterService extends BaseService {
	public SbasSaveClientRosterOut saveClientRoster(SbasSaveClientRosterIn in) throws Exception {
		checkStringNotBlank(in, "clientCode,clientName");

		SbasCusDef oldClient = selectWithPk(SbasCusDef.class, in.getUserOrgCode(), in.getClientCode());
		SbasSupDef client = new SbasSupDef();
		if (oldClient != null) {
			if (new Character('D').equals(oldClient.getStatus())) {
				throw new BizException("SBAS-0004", new FieldMap("supCode", oldClient.getClientCode()));
			}

			if (new Character('D').equals(in.getStatus())) {
				oldClient.setStatus(in.getStatus());

				update(oldClient, "status");
			} else {
				copyProperties(oldClient, client);
				copyProperties(in, client);
				if (in.getStatus() == null) {
					client.setStatus('N');
				}

				update(client);
			}
		} else {
			copyProperties(in, client);
			client.setStatus('N');

			insert(client);
		}

		SbasSaveClientRosterOut out = new SbasSaveClientRosterOut();

		return out;
	}
}